﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Cms.Core.Application.IService.TokenInfo;
using Cms.Core.Application.IService.UserInfo;
using Cms.Core.Application.Secutiry.Token;
using Cms.Core.Infrastructure.AuthHelper;
using Microsoft.Extensions.DependencyInjection;

namespace Cms.Core.Application.Secutiry.Base
{
    /// <summary>
    /// 
    /// </summary>
    public class OnlineTokenProvider : IOnlineTokenProvider
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="provider"></param>
        /// <param name="userId"></param>
        /// <param name="token"></param>
        /// <param name="expirationTime"></param>
        /// <returns></returns>
        public async Task<OnlineToken> Create(IServiceProvider provider, Guid userId, string token,DateTime expirationTime)
        {
           var tokenService = provider.GetService<ITokenService>();
           var tokenDto=  await  tokenService.GetToken(token);

            if (tokenDto is null)
            {
                return null;
            }
            var permissionRequirement = provider.GetService<PermissionRequirement>();
            return new OnlineToken
            {
                UserId = userId,
                Token = token,
                ExpirationTime = expirationTime,
                RefreshTime = expirationTime.AddMinutes(permissionRequirement.RefreshCoefficients)
            };
        }
    }
}
